package com.example.whatrubbish.controller;

import com.example.whatrubbish.model.Question;
import com.example.whatrubbish.service.QuestionService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;

    @GetMapping("")
    public JsonResult getTen() {
        return new JsonResult(Constants.STATUS_SUCCESS, "success", questionService.selectRand());
    }

    @GetMapping("/get")
    public JsonResult get(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          @RequestParam("title") String title) {
        return new JsonResult(Constants.STATUS_SUCCESS, "success", questionService.selectByCondition(pageIndex, pageSize, title));
    }

    @PostMapping("")
    public JsonResult insert(@RequestBody Question question) {
        return new JsonResult(Constants.STATUS_SUCCESS, "success", questionService.insert(question));
    }

    @PutMapping("")
    public JsonResult update(@RequestBody Question question) {
        return new JsonResult(Constants.STATUS_SUCCESS, "success", questionService.update(question));
    }

    @DeleteMapping("")
    public JsonResult delete(@RequestParam(value = "id") Integer id) {
        return new JsonResult(Constants.STATUS_SUCCESS, "success", questionService.delete(id));
    }
}
